package pikes.samples.countries.ui.peak;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.ecma.ExpressionStatement;
import pikes.ecma.IExpression;
import pikes.ecma.SourceElement;
import pikes.ecma.SourceElements;
import pikes.peak.RequestIndependentProperty;
import pikes.peak.RequestProperty;

public class DynamicSourceElements implements RequestProperty<SourceElements> {

	private List<RequestProperty<? extends SourceElement>> sourceElementList = new LinkedList<RequestProperty<? extends SourceElement>>();

	public void addSourceElement(RequestProperty<? extends SourceElement> sourceElement) {
		sourceElementList.add(sourceElement);
	}

	public void addSourceElement(SourceElement sourceElement) {
		this.addSourceElement(new RequestIndependentProperty<SourceElement>(sourceElement));
	}

	public void addSourceElement(IExpression expression) {
		this.addSourceElement(new ExpressionStatement(expression));
	}

	public SourceElements getValue(Map model, RequestContext requestContext) throws Exception {
		SourceElements sourceElements = new SourceElements();

		for (RequestProperty<? extends SourceElement> sourceElement : sourceElementList) {
			sourceElements.add(sourceElement.getValue(model, requestContext));
		}

		return sourceElements;
	}

}